package vip.youngboy.freejump.domain.process;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.task.Task;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import vip.youngboy.freejump.constant.TaskActionType;
import vip.youngboy.freejump.domain.FileInfo;
import vip.youngboy.freejump.domain.FlowRequestParam;
import vip.youngboy.freejump.domain.TaskActionModel;

import javax.persistence.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Data
@ApiModel(
        value = "任务流转状态意见附件表",
        description = "记录流程流转中的状态，用户意见和附件"
)
@Entity
@Table(name = "ACT_TASK_OPINION")
@DynamicUpdate
@DynamicInsert
public class TaskOpinion {

    @Id
    @GenericGenerator(name = "file-uuid", strategy = "uuid")
    @GeneratedValue(generator = "file-uuid")
    @Column(name = "ID")
    private String id;

    @Column(name = "INSTANCE_ID")
    private String instanceId;

    @Column(name = "P_INSTANCE_ID")
    private String pInstanceId;

    @Column(name = "TASK_ID")
    private String taskId;

    @Column(name = "TASK_NAME")
    private String taskName;

    @Column(name = "TASK_DEF_KEY")
    private String taskDefKey;

    @Column(name = "CANDIDATE")
    private String candidate;

    @Column(name = "ASSIGN")
    private String assign;

    @Column(name = "OPINION")
    private String opinion;

    @Column(name = "TASK_STATUS")
    private String taskStatus;

    @Column(name = "FORM_ID")
    private String formId;

    @Column(name = "CREATE_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Column(name = "END_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;


    @Column(name = "DUR_MS")
    private Long durMs;

    @Column(name = "FILES")
    private String files;

    public static TaskOpinion createStartTaskOpinion(TaskEntity taskEntity) {
        TaskOpinion taskOpinion = new TaskOpinion();
        LocalDateTime now = LocalDateTime.now();
        taskOpinion.setCreateTime(now);
        taskOpinion.setTaskId(taskEntity.getId());
        String actionName = TaskActionType.TODO.getName();
        taskOpinion.setTaskStatus(actionName);
        taskOpinion.setTaskName(taskEntity.getName());
        taskOpinion.setTaskDefKey(taskEntity.getTaskDefinitionKey());
        taskOpinion.setFormId(taskEntity.getFormKey());
        return taskOpinion;
    }

    public static TaskOpinion createEndTaskOpinion(TaskOpinion startOpinion, TaskActionModel taskActionModel, String assign) {
        FlowRequestParam flowRequestParam = taskActionModel.getFlowRequestParam();
        Task task = taskActionModel.getTask();
        List<FileInfo> files = flowRequestParam.getFiles();
        String jsonStr = JSON.toJSONString(files);
        startOpinion.setAssign(assign);
        LocalDateTime start = startOpinion.getCreateTime();
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(start, now);
        startOpinion.setDurMs(duration.getSeconds());
        startOpinion.setEndTime(now);
        startOpinion.setOpinion(flowRequestParam.getOpinion());
        startOpinion.setFiles(jsonStr);
        startOpinion.setTaskName(task.getName());
        String actionName = TaskActionType.fromKey(flowRequestParam.getAction()).getName();
        startOpinion.setTaskStatus(actionName);
        return startOpinion;
    }


}